package com.qc31.baselib.utils

import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.text.TextUtils
import com.qc31.baselib.R


/** @Author pc_hfjs @Date 2022/2/10 10:59 @Desc */
object NaviUtil {
    //百度地图
    const val baidu: String = "com.baidu.BaiduMap"

    //高德地图
    const val gaode: String = "com.autonavi.minimap"

    //腾讯地图
    const val tencent: String = "com.tencent.com.qc31.comlib.map"

    //Google地图
    const val google: String = "com.google.android.apps.maps"


    /**
     * 检查手机上是否安装了指定的软件
     *
     * @param context
     * @param packageName：应用包名
     * @return
     */
    private fun isAvilible(context: Context, packageName: String): Boolean {
        //获取packageManager
        val packageManager = context.packageManager
        //获取全部已安装程序的包信息
        val packageInfos = packageManager.getInstalledPackages(PackageManager.GET_ACTIVITIES)
        //用于存储全部已安装程序的包名
        val packageNames: MutableList<String> = ArrayList()
        //从packageInfos中将包名字逐一取出，压入pName list中
        for (i in packageInfos.indices) {
            val packName = packageInfos[i].packageName
            packageNames.add(packName)
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName)
    }

    private fun installed(context: Context, pkgName: String): Boolean {
        if (TextUtils.isEmpty(pkgName)) {
            return false
        }
        try {
            context.packageManager.getPackageInfo(pkgName, 0)
        } catch (x: Exception) {
            return false
        }
        return true
    }


    fun startToBaiduMap(context: Context, endPoint: String, endLatLng: String) {
        if (installed(context, baidu)) {
            val intent = Intent() // 驾车路线规划
            val uri =
                "baidumap://com.qc31.comlib.map/direction?destination=name:$endPoint|latlng:$endLatLng&coord_type=bd09ll&mode=driving&src=andr.qc31.gps_navi"
            intent.data = Uri.parse(uri)
            context.startActivity(intent)
        } else {
            R.string.desc_no_map_baidu.toast(context)
        }

    }

    fun startToGaodeMap(context: Context, endPoint: String, endLat: String, endLng: String) {
        if (installed(context, gaode)) {
            val intent = Intent("android.intent.action.VIEW")
            intent.addCategory("android.intent.category.DEFAULT")
            intent.data =
                Uri.parse("amapuri://route/plan/?dlat=$endLat&dlon=$endLng&dname=$endPoint&dev=0&t=0")
            intent.setPackage("com.autonavi.minimap")
            context.startActivity(intent)
        } else {
            R.string.desc_no_map_gaode.toast(context)
        }
    }

    fun startGoogleMap(context: Context, endLat: String, endPoint: String) {
        if (isAvilible(context, google)) {
            val gmmIntentUri = Uri.parse("google.navigation:q=$endLat,$endPoint")
            val mapIntent = Intent(
                Intent.ACTION_VIEW,
                gmmIntentUri
            )
            mapIntent.setPackage(google)
            context.startActivity(mapIntent)
        } else {
            R.string.desc_no_map_google.toast(context)
        }
    }

}